package xin.yangshuai.basic01.sse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Controller
@RequestMapping("/sse")
public class SseEmitterController {

    @Autowired
    private SseEmitterService sseEmitterService;

    @GetMapping("/index")
    public String index() {
        return "sse/index";
    }

    @GetMapping("/create")
    @ResponseBody
    public SseEmitter create(String clientId) {
        return sseEmitterService.create(clientId);
    }

    @GetMapping("/send")
    @ResponseBody
    public String send(String message) {
        sseEmitterService.sendMsgToAll(message);
        return message;
    }

}
